using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using BlueGameComponent;

namespace XuperMan
{
    class GameScreen : BaseScreen
    {
        public bool Pause { get; set; }
        public bool isPause = false;

        public bool GameOver { get; set; }
        public bool isGameOver = false;

        public int Score 
        {
            get { return nScore; }
        }
        int nScore = 0;

        Texture2D bg;
        Player player = new Player();

        ParallaxingBackground Parallax;

        public GameScreen()
        {
            TransitionOnTime = TimeSpan.FromSeconds(0.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.6);
            AudioManager.StopMusic();
            AudioManager.PlayMusic("Sounds/gameMusic");

            Parallax = new ParallaxingBackground();
            EnabledGestures = GestureType.Tap | GestureType.FreeDrag;
        }

        public override void LoadContent()
        {
            base.LoadContent();
            bg = ScreenManager.Game.Content.Load<Texture2D>("Textures/mainbackground");
            Parallax.Initialize(true, ScreenManager.ViewPort.Width);
            Parallax.AddLayer(ScreenManager.Game.Content.Load<Texture2D>("Textures/mainbackground"), -1.0f);
            Parallax.AddLayer(ScreenManager.Game.Content.Load<Texture2D>("Textures/bgLayer1"), -4.0f);
            Parallax.AddLayer(ScreenManager.Game.Content.Load<Texture2D>("Textures/bgLayer2"), -7.0f);
            //player.PlayerAnimation.Initialize(ScreenManager.Game.Content.Load<Texture2D>("Textures/shipAnimation"), 8);
            player.PlayerAnimation.Initialize(ScreenManager.Game.Content.Load<Texture2D>("Textures/xuperman"), 1);
            
            player.PlayerAnimation.Active = true;
            player.PlayerAnimation.Looping = true;

            //xuperman = ScreenManager.Game.Content.Load<Texture2D>("Textures/xuperman");
            
        }

        public override void HandleInput(InputState input)
        {
            base.HandleInput(input);

            if (input == null)
            {
                throw new ArgumentNullException("input");
            }

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {

                PauseGame();
            }
            if (!IsActive)
            {
                return;
            }
            // Return to the main menu when a tap gesture is recognized
            foreach (GestureSample gesture in input.Gestures)
            {
                if (gesture.GestureType == GestureType.FreeDrag)
                {
                    player.PlayerAnimation.Position += gesture.Delta*2;
                }
            }
        }
       
        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

            if (Pause)
            {
                return;
            }
            Parallax.Update();
            if (GameOver)
            {
                return;
            }

            

            nScore++;
            player.Update(gameTime);
            
            if (0 == nScore % 1000)
            {
                //ScreenManager.AddScreen(new GameOverScreen(this),null);
            }
        }
         
        public override void Draw(GameTime gameTime)
        {
            //base.Draw(gameTime);
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            SpriteFont font = ScreenManager.Font;
            
            spriteBatch.Begin();

            //spriteBatch.Draw(bg, new Vector2(0, 0), Color.White);
            Parallax.Draw(spriteBatch);
            spriteBatch.DrawString(ScreenManager.Font, nScore.ToString(), new Vector2(0, 0), Color.Black);
            player.Draw(spriteBatch);

            spriteBatch.End();
        }

        public void EndGame()
        {
            ScreenManager.AddScreen(new GameOverScreen(this), null);
        }
        public void PauseGame()
        {
            if (GameOver)
            {
                return;
            }
            ScreenManager.AddScreen(new PauseScreen(this), null);
        }
        

               
    }
}
